package br.skytef.stmanager.beans;

import java.io.IOException;
import java.sql.SQLException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import jxl.read.biff.BiffException;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import br.skytef.stmanager.entities.ParceiroEntity;
import br.skytef.stmanager.utils.ImportandoExternos;
import br.skytef.stmanager.utils.PlanGsurf;
import br.skytef.stmanager.utils.PlanLyra;
import br.skytef.stmanager.utils.PlanSE;

/**
 * Class ImportarVPN Controller
 */

@ManagedBean(name="ImportarVPNBean")
@SessionScoped
public class ImportarVPNBean {

	/**
	 * Campos com seus respactivos Getters e Setters.
	 */
	public ParceiroEntity parceiroPlan; 
	public ParceiroEntity getParceiroPlan() {return parceiroPlan;}
	public void setParceiroPlan(ParceiroEntity parceiroPlan) {this.parceiroPlan = parceiroPlan;}

	public RequestContext getContext() {return context;}
	public void setContext(RequestContext context) {this.context = context;}
	private RequestContext context;

	static PlanLyra  planilhaL;
	static PlanGsurf planilhaG;
	static PlanSE planilhaS;
	/**
	 * Construtor
	 * @throws IOException
	 */
	public ImportarVPNBean() throws IOException {
		super();
	}
	/**
	 * Método para importação das planilhas.
	 * @param fileUploadEvent
	 * @throws IOException
	 * @throws BiffException
	 * @throws SQLException 
	 * @throws InterruptedException 
	 */

	public void doUpload (FileUploadEvent fileUploadEvent) throws IOException, BiffException, SQLException, InterruptedException{
		UploadedFile uploadedFile = fileUploadEvent.getFile();
		String fileNameUploaded = uploadedFile.getFileName();
		int ult = fileNameUploaded.length()-1;
		if(fileNameUploaded.charAt(ult) =='s' &&
				fileNameUploaded.charAt(ult-1) =='l' &&
				fileNameUploaded.charAt(ult-2) =='x' &&
				fileNameUploaded.charAt(ult-3) =='.'){
			planilhaL = new PlanLyra(uploadedFile.getInputstream());
			planilhaG = new PlanGsurf(uploadedFile.getInputstream());
			planilhaS = new PlanSE(uploadedFile.getInputstream());
			
			if (planilhaL.validar()){
				ImportandoExternos inserir3 = new ImportandoExternos();
				inserir3.setPlanilhaL(planilhaL);
				inserir3.setPlanilhaG(null);
				inserir3.setPlanilhaS(null);
				inserir3.setNomePlanilha(fileNameUploaded);
				
				Thread t3 = new Thread(inserir3);
				t3.start();
				t3.join();
				
				String infoAboutFile = "<br/> Arquivo recebido: <b>"+fileNameUploaded +"</b><br/>";
				FacesContext facesContext = FacesContext.getCurrentInstance(); 
				facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Importação realizada com sucesso ", infoAboutFile));
			}else if (planilhaG.validar()){
				ImportandoExternos inserir4 = new ImportandoExternos();
				inserir4.setPlanilhaG(planilhaG);
				inserir4.setPlanilhaL(null);
				inserir4.setPlanilhaS(null);
				inserir4.setNomePlanilha(fileNameUploaded);
				
				Thread t4 = new Thread(inserir4);
				t4.start();
				t4.join();
				
				String infoAboutFile = "<br/> Arquivo recebido: <b>"+fileNameUploaded +"</b><br/>";
				FacesContext facesContext = FacesContext.getCurrentInstance(); 
				facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Importação realizada com sucesso ", infoAboutFile));

			}else if (planilhaS.validar()){
				ImportandoExternos inserir5 = new ImportandoExternos();
				inserir5.setPlanilhaS(planilhaS);
				inserir5.setPlanilhaG(null);
				inserir5.setPlanilhaL(null);
				inserir5.setNomePlanilha(fileNameUploaded);
				
				Thread t5 = new Thread(inserir5);
				t5.start();
				t5.join();
				
				String infoAboutFile = "<br/> Arquivo recebido: <b>"+fileNameUploaded +"</b><br/>";
				FacesContext facesContext = FacesContext.getCurrentInstance(); 
				facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Importação realizada com sucesso ", infoAboutFile));

			}else{
				FacesContext facesContext = FacesContext.getCurrentInstance(); 
				facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Falha" , "planilha invália!"));
			}

		}else{
			FacesContext facesContext = FacesContext.getCurrentInstance(); 
			facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Falha", "Formato de arquivo inválido"));
		}
	}
}
